/*!
 * Ext JS Library 3.4.0
 * Copyright(c) 2006-2011 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */

//
// Extend the XmlTreeLoader to set some custom TreeNode attributes specific to our application:
//
Ext.app.BookLoader = Ext.extend(Ext.ux.tree.XmlTreeLoader, {
	processAttributes : function(attr) {
		if (attr.flag) { // is it an author node?

			// Set the node text that will show in the tree since our raw data
			// does not include a text attribute:
			attr.text = attr.title;

			attr.loaded = true;
			attr.expanded = true;
		} else if (attr.id) { // is it a book node?
			attr.text = attr.title;
			attr.leaf = true;
		}
	}
});

Ext.onReady(function() {
	var panelCache = {};

	var getOrCachePanel = function(id) {
		if (panelCache[id]) {
			return panelCache[id];
		}

		var resultId = '';
		var resultPanel;

		switch (id) {
		case 'userInfo-Panel' : 
			resultId = 'userInfo-Panel';
			resultPanel = new crs.common.UserFormPanel();
			break;
		case 'pendingApplicantReview-Panel':
			resultId = 'pendingApplicantReview-Panel';
			resultPanel = new crs.grid.UserReviewGridPanel();
			break;
		case 'pendingApplicationReview-Panel':
			resultId = 'pendingApplicationReview-Panel';
			resultPanel = new crs.grid.ApplyingFormReviewGridPanel();
			break;
		case 'pendingAlterationReview-Panel':
			resultId = 'pendingAlterationReview-Panel';
			resultPanel = new crs.grid.AlterationFormReviewGridPanel();
			break;
		case 'reviewedApplicants-Panel':
			resultId = 'reviewedApplicants-Panel';
			resultPanel = new crs.grid.ReviewedApplicantGridPanel();
			break;
		case 'reviewedApplications-Panel':
			resultId = 'reviewedApplications-Panel';
			resultPanel = new crs.grid.ReviewedApplicationGridPanel();
			break;
		case 'reviewedAlterations-Panel':
			resultId = 'reviewedAlterations-Panel';
			resultPanel = new crs.grid.ReviewedAlterationGridPanel();
			break;
		case 'myRegisterations-Panel':
			resultId = 'myRegisterations-Panel';
			resultPanel = new crs.grid.MyRegisterationGridPanel();
			break;
		case 'r21Applied-Panel':
			resultId = 'r21Applied-Panel';
			resultPanel = new crs.grid.R21AppliededGridPanel();
			break;
		case 'r41Applied-Panel':
			resultId = 'r41Applied-Panel';
			resultPanel = new crs.grid.R41AppliedGridPanel();
			break;

		/*
		 * case 'pendingApplicantReview-Panel': resultId =
		 * 'pendingApplicantReview-Panel'; resultPanel = new
		 * crs.grid.UserReviewGridPanel(); break; case
		 * 'pendingApplicantReview-Panel': resultId =
		 * 'pendingApplicantReview-Panel'; resultPanel = new
		 * crs.grid.UserReviewGridPanel(); break; case
		 * 'pendingApplicantReview-Panel': resultId =
		 * 'pendingApplicantReview-Panel'; resultPanel = new
		 * crs.grid.UserReviewGridPanel(); break;
		 */

		}

		panelCache[resultId] = resultPanel;
		return resultPanel;
	};

	function doLogout() {
		Ext.Ajax.request({
			url : 'user_logout.do',
			success : function(resp) {
				var rp = Ext.decode(resp.responseText);
				if (rp.success == true) {
					Ext.MessageBox.alert('提示', '登出成功！');
					location.reload();
				} else if (rp.success == false) {
					Ext.MessageBox.alert('错误', '错误信息:' + rp.resultMsg);
				}
			},
			scope : this

		});
	}

	var tabs = new Ext.TabPanel({
		id : 'tab_contentPanel',
		region : 'center',
		margins : '3 3 3 0',
		activeTab : 0,
		defaults : {
			autoScroll : true
		},
		listeners : {
			remove : function(tp, c) {
				c.hide();
			}
		},
		autoDestroy : false,

		items : [ {
			xtype : 'panel',
			title : '欢迎信息',
			closable : true,
			bodyStyle : 'background:url(images/welcome.jpg) no-repeat 5px 5px;'
		}],
		tbar : new Ext.Toolbar({
			items : [ '->', {
				text : '退出登录',
				iconCls : 'delete',
				handler : doLogout,
				scope : this
			} ]
		})
	});
	// Panel for the west
	var nav = new Ext.Panel({
		title : '主菜单',
		region : 'west',
		split : true,
		width : 250,
		collapsible : true,
		margins : '3 0 3 3',
		cmargins : '3 3 3 3',
		items : [ {
			xtype : 'treepanel',
			id : 'tree-panel',
			region : 'center',
			margins : '2 2 0 2',
			autoScroll : true,
			rootVisible : false,
			root : new Ext.tree.AsyncTreeNode(),

			// Our custom TreeLoader:
			loader : new Ext.app.BookLoader({
				dataUrl : getFunctionList()
			}),

			listeners : {
				'render' : function(tp) {
					tp.getSelectionModel().on('selectionchange',
							function(tree, node) {
								// var el = Ext.getCmp('details-panel').body;
								if (node && node.leaf) {
									// var _targetPanel =
									// getOrCachePanel(node.id);
									if (node.id == 'r21Apply-Panel') {
										window.open('capply_preSubmit.do');
									} else if (node.id == 'r41Apply-Panel') {
										window.open('calter_preSubmit.do');
									} else {
										tabs.add(getOrCachePanel(node.id));
									}
									// tpl.overwrite(el, node.attributes);
								}
							});
				}
			}
		} ]
	});

	var mainPanel = new Ext.Viewport({
		title : 'Layout Window',
		enableTabScroll : true,
		// border:false,
		layout : 'border',

		items : [ {
			xtype : 'panel',
			bodyStyle : 'background:url(images/top.jpg) repeat;',
			region : 'north',
			height : 82,
			html: '<img src="images/top002.jpg" width="499" height="82"/>'
		},nav, tabs ]
	});
	// mainPanel.show();
});